package cn.mxz.controller;


import cn.mxz.pojo.*;
import cn.mxz.service.ProductCommentsService;
import cn.mxz.service.ProductService;
import cn.mxz.service.ProductSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("GoodDetails")
@CrossOrigin
public class GoodDetailsController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductCommentsService productCommentsService;
    @Autowired
    private ProductSkuService productSkuService;

    @GetMapping("getDetails")
    public ResultData<ProductParams> getDetails(String product_id){
        ProductParams productParams = productService.getProductParamsById(product_id);
        if(productParams==null){
            return new ResultData(200,"未找到对应id的产品详情");
        }
        return new ResultData(0,"已找到对应id的产品详情",productParams);
    }

    @GetMapping("getMsg")
    public ResultData<ProductParams> getMsg(String product_id){
        List<ProductComments> productComments = productCommentsService.getProductComments(product_id);
        if(productComments==null){
            return new ResultData(201,"暂无评论");
        }
        return new ResultData(0,"已找到对应id的产品评论",productComments);
    }

    @GetMapping("getSku")
    public ResultData<ProductSku> getSku(String product_id){
        List<ProductSku> productSku = productSkuService.getProductSku(product_id);
        if(productSku==null){
            return new ResultData(202,"暂无规格");
        }
        return new ResultData(0,"已找到对应id的产品规格",productSku);
    }


//产品名字模糊查询
    @GetMapping("find")
    public ResultData<ProductParams> findProducts(String text){
        List<Product> productByName = productService.findProductByName(text);
        if(productByName.size()==0){
            return new ResultData(201,"暂无产品");
        }
        return new ResultData(0,"已找到相关产品",productByName);
    }

}
